﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Data.SQLite;

namespace KeyboardImageViewer
{
    /// <summary>
    /// Interaction logic for Settings.xaml
    /// </summary>
    public partial class Settings : Window
    {
        public Settings(MainWindow window)
        {
            this.Owner = window;

            InitializeComponent();

            ImageManager.Config.ApplyWindowSize("Settings", this);

            SettingsTab currentTab = (SettingsTab)ImageManager.Config.GetConfigInt("LastSettingsTab");

            switch (currentTab)
            {
                case SettingsTab.General:
                    this.mainTabControl.SelectedItem = this.generalTab;
                    break;
                case SettingsTab.Library:
                    this.mainTabControl.SelectedItem = this.libraryTab;
                    break;
                default:
                    break;
            }

            this.automaticUpdatesBox.IsChecked = ImageManager.Config.UpdatesEnabled;

            this.trackDuplicatesBox.IsChecked = ImageManager.Config.TrackDuplicates;
            this.trackImageDuplicatesBox.IsChecked = ImageManager.Config.TrackImageDuplicates;

            this.overlayInfobarBox.IsChecked = ImageManager.Config.OverlayInfobar;
            this.slideshowDelayBox.Text = ImageManager.Config.SlideshowDelaySeconds.ToString();

            this.multiFolderPicker.DirectorySet = ImageManager.Config.SourceDirectories;
            this.multiFolderPicker.ExpandedDirectories = ImageManager.Config.ExpandedDirectories;

            FullscreenStart fullscreenStart = ImageManager.Config.FullscreenStartSetting;
            switch (fullscreenStart)
            {
                case FullscreenStart.RememberLast:
                    this.fullscreenRemember.IsChecked = true;
                    break;
                case FullscreenStart.AlwaysFullscreen:
                    this.fullscreenFullscreen.IsChecked = true;
                    break;
                case FullscreenStart.AlwaysWindowed:
                    this.fullscreenWindowed.IsChecked = true;
                    break;
                default:
                    break;
            }

            if (!ImageManager.Config.TrackDuplicates)
            {
                this.trackImageDuplicatesBox.IsEnabled = false;
            }
        }

        public bool UpdatesEnabled
        {
            get { return (bool)this.automaticUpdatesBox.IsChecked; }
        }

        public bool TrackDuplicates
        {
            get { return (bool)this.trackDuplicatesBox.IsChecked; }
        }

        public bool TrackImageDuplicates
        {
            get { return (bool)this.trackImageDuplicatesBox.IsChecked; }
        }

        public DirectorySet SourceDirectories
        {
            get { return this.multiFolderPicker.DirectorySet; }
        }

        public List<string> MovedDirectories
        {
            get { return this.multiFolderPicker.MovedDirectories; }
        }

        public string SlideshowDelaySeconds
        {
            get { return this.slideshowDelayBox.Text; }
        }

        public FullscreenStart FullscreenStart
        {
            get
            {
                if (this.fullscreenRemember.IsChecked == true)
                {
                    return FullscreenStart.RememberLast;
                }

                if (this.fullscreenFullscreen.IsChecked == true)
                {
                    return FullscreenStart.AlwaysFullscreen;
                }

                return FullscreenStart.AlwaysWindowed;
            }
        }

        public bool OverlayInfobar
        {
            get { return (bool)this.overlayInfobarBox.IsChecked; }
        }

        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.multiFolderPicker.HasDirectories)
            {
                this.DialogResult = true;
            }
            else
            {
                MessageBox.Show("You must pick at least one directory.");
            }
        }

        private void trackDuplicatesBox_Checked(object sender, RoutedEventArgs e)
        {
            this.trackImageDuplicatesBox.IsEnabled = true;
        }

        private void trackDuplicatesBox_Unchecked(object sender, RoutedEventArgs e)
        {
            this.trackImageDuplicatesBox.IsEnabled = false;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            using (SQLiteTransaction transaction = ImageManager.Connection.BeginTransaction())
            {
                ImageManager.Config.SaveWindowSize("Settings", this);

                if (this.mainTabControl.SelectedItem == this.generalTab)
                {
                    ImageManager.Config.SetConfigValue("LastSettingsTab", (int)SettingsTab.General);
                }
                else if (this.mainTabControl.SelectedItem == this.libraryTab)
                {
                    ImageManager.Config.SetConfigValue("LastSettingsTab", (int)SettingsTab.Library);
                }

                ImageManager.Config.ExpandedDirectories = this.multiFolderPicker.ExpandedDirectories;

                transaction.Commit();
            }
        }

        private void slideshowDelayBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            foreach (char c in e.Text)
            {
                if (!char.IsNumber(c))
                {
                    e.Handled = true;
                    return;
                }
            }
        }

        private void slideshowDelayBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                e.Handled = true;
            }
        }

        private void slideshowDelayBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Dispatcher.BeginInvoke(new Action(() => this.slideshowDelayBox.SelectAll()));
        }
    }
}
